package junits.study_junit_fixture;

import com.hedl.practice.diyJava.junit.study_junit_fixture.test.Calculator;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertEquals;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Main {


    @Autowired
    Calculator calculator;

    /**
     * 这个类的功能很简单，但是测试的时候，我们要先初始化对象，我们不必在每个测试方法中都写上初始化代码，
     * 而是通过@BeforeEach来初始化，通过@AfterEach来清理资源
     */

    @BeforeEach
    public void setUp(){
        this.calculator = new Calculator();
    }

    @AfterEach
    public void tearDown(){
        this.calculator = null;
    }

    @Test
    public void testAdd(){
        assertEquals(100,this.calculator.add(100));
        assertEquals(150,this.calculator.add(50));
        assertEquals(130,this.calculator.add(-20));

    }

    @Test
    public void testSub(){
        assertEquals(-100,this.calculator.add(100));
        assertEquals(-150,this.calculator.add(50));
        assertEquals(-130,this.calculator.add(-20));
    }

}
